﻿using DevExpress.XtraEditors;
using Lamex.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mIE.Calculate
{
    /* 1、调用LGC_VAL_BOM得到需要计算的Datatable
     * 2、
     */
    public class LGC_VAL_BOM_P73
    {
        // "A、如果包含P73
        //a、子程序1、通过[BOM].[DRAWING_NO]条件，传参【P73_装铝材_L1000】、【P73_装铝材_L1001】、【1000】，返回
        //b、子程序2、通过[BOM].[COMPONENT]条件，传参【P73_装L码，P73_装顶杆固定码，P73_装机米，P73_装弹片，P73_装挂码，P73_装分线码，P73_打螺丝，P73_装走线胶盖，P73_套胶袋】，返回

        //c、通过[BOM].[MATERIAL_GROUP]及[COMPONENT]条件，返回
        //返回逻辑
        //遍历抽出BOM数据中的[BOM].[MATERIAL_GROUP]
        //IF [BOM].[MATERIAL_GROUP]=28
        //  根据[BOM].[COMPONENT]抽出尺寸数据，表为[IE_Part_SizeParmList].[WIDTH]
        //  IF [WIDTH]<=1200
        //    【P73_装玻璃_W1200】=【P73_装玻璃_W1200】+[BOM].[SINGLE_QTY]
        //  ELSE
        //    【P73_装玻璃_W1201】=【P73_装玻璃_W1201】+[BOM].[SINGLE_QTY]
        //  END IF
        //END IF"

        public static DataTable LGC_VAL_BOM_P73_VALUE(DataTable dt_BOM, string DWG_No, string Part_No, string Process_Code, string WORKTIME_LOGIC_CODE, out string Error_Code)
        {
            string errorMsg = DWG_No + Part_No + Process_Code;
            Error_Code = "";
            try
            {
                //a.子程序1
                DataTable dt1 = Calculate.LGC_VAL_BOM_SUB1.LGC_VAL_BOM_SUB1_VALUE("P73_装铝材_L1000", "P73_装铝材_L1001", 1000, dt_BOM, DWG_No, Part_No, Process_Code, WORKTIME_LOGIC_CODE);
                //b.子程序2
                DataTable dt2 = Calculate.LGC_VAL_BOM_SUB2.LGC_VAL_BOM_SUB2_VALUE(dt_BOM, DWG_No, Part_No, Process_Code, WORKTIME_LOGIC_CODE,
                    "P73_装L码,P73_装顶杆固定码,P73_装机米,P73_装弹片,P73_装挂码,P73_装分线码,P73_打螺丝,P73_装走线胶盖,P73_套胶袋");
                //c
                DataTable dt3 = Common.GetTable.GetTable_Feature();
                double Feature_CheckA_VALUE = 0, Feature_CheckB_VALUE = 0;
                if (dt_BOM.Rows.Count > 0)
                {
                    for (int i = 0; i < dt_BOM.Rows.Count; i++)
                    {
                        //图纸号不为空才执行
                        if (dt_BOM.Rows[i]["MATERIAL_GROUP"].ToString() != "82")
                            continue;
                        //根据BOM部件号COMPONENT查询得到尺寸参数L,【IE_Part_SizeParmList】
                        DataTable dtTemp = BaseBuildSql.GetDataTableBySql("select WIDTH from IE_Part_SizeParmList where PART_NO='" + dt_BOM.Rows[i]["MATERIAL_NO"].ToString() + "'");
                        if (dtTemp.Rows.Count <= 0 || dtTemp.Rows[0]["WIDTH"].ToString().Trim().Length <= 0)
                        {
                            Error_Code = "该号码尺寸参数表【宽】无数值！";
                            continue;
                        }
                        double W = dtTemp.Rows[0]["WIDTH"].ToString().Length > 0 ?
                            Convert.ToDouble(dtTemp.Rows[0]["WIDTH"].ToString()) : 0;
                        double SINGLE_QTY = dt_BOM.Rows[i]["SINGLE_QTY"].ToString().Length > 0 ?
                            Convert.ToDouble(dt_BOM.Rows[i]["SINGLE_QTY"].ToString()) : 0;
                        if (W <= 1200)
                        {
                            Feature_CheckA_VALUE = Feature_CheckA_VALUE + SINGLE_QTY;
                        }
                        else
                            Feature_CheckB_VALUE = Feature_CheckB_VALUE + SINGLE_QTY;
                    }
                    if (Feature_CheckA_VALUE > 0)
                    {
                        DataRow dr = dt3.NewRow();
                        dr["DWG_NO"] = DWG_No;
                        dr["PART_NO"] = Part_No;
                        dr["PROCESS_CODE"] = Process_Code;
                        dr["FEATURE_NAME"] = "P73_装玻璃_W1200";
                        dr["FEAT_VALUE"] = Feature_CheckA_VALUE;
                        dr["LABOR_VALUE"] = Calculate.LGC_TIM_VTA.LGC_TIM_VTA_VALUE(DWG_No, Part_No, "P73_装玻璃_W1200", "", Feature_CheckA_VALUE);//根据特征定义表工时字段计算
                        dr["UPDATE_TIME"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                        dr["UPDATE_USER"] = Lamex.Common.Loginer.CurrentUser.AccountName;
                        dt3.Rows.Add(dr);
                    }
                    if (Feature_CheckB_VALUE > 0)
                    {
                        DataRow dr1 = dt3.NewRow();
                        dr1["DWG_NO"] = DWG_No;
                        dr1["PART_NO"] = Part_No;
                        dr1["PROCESS_CODE"] = Process_Code;
                        dr1["FEATURE_NAME"] = "P73_装玻璃_W1201";
                        dr1["FEAT_VALUE"] = Feature_CheckB_VALUE;
                        dr1["LABOR_VALUE"] = Calculate.LGC_TIM_VTA.LGC_TIM_VTA_VALUE(DWG_No, Part_No, "P73_装玻璃_W1201", "", Feature_CheckB_VALUE);//根据特征定义表工时字段计算
                        dr1["UPDATE_TIME"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
                        dr1["UPDATE_USER"] = Lamex.Common.Loginer.CurrentUser.AccountName;
                        dt3.Rows.Add(dr1);
                    }
                }
                //合并3个Datatable
                DataTable dtResult = new DataTable();
                dtResult.Merge(dt1);
                dtResult.Merge(dt2);
                dtResult.Merge(dt3);

                return dtResult;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(errorMsg + ex.Message);
                return null;
            }
        }
        
}
}
